Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router#verbs() #169

Closed
wants to merge 1 commit into from
Closed

Router#verbs() #169

wants to merge 1 commit into from

Conversation

vanodevium
Copy link

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

xo linting is broken and I have no idea how to fix it


Route#verbs()

First of all, I created this method just as an idea.

In fact, this method will be useful in cases where the callback is just an anonymous function and you need to assign it to several HTTP methods.

router.verbs(['get', 'post'], '/endpoint', function (ctx) {
	ctx.body = "Ok"
})

I absolutely understand that it could be done this way:

router.get('/endpoint', middleware());
router.post('/endpoint', middleware());

but for dynamic programming this method would be much more useful.

@etroynov
Copy link
Contributor

etroynov commented Sep 9, 2023

I think the case where we have to do the same job using multiple methods is rare.

@3imed-jaberi 3imed-jaberi self-assigned this Aug 15, 2024
@3imed-jaberi 3imed-jaberi self-requested a review August 15, 2024 00:18
@3imed-jaberi
Copy link
Member

Thanks @vanodevium for your contribution, as @etroynov mentioned this use case is very rare and you can create HOF as a helper, something like;

function withMultiVerbs(Router) {
 /**
  * Register route with concrete verbs.
  *
  * @param {Array} verbs
  * @param {String} name Optional.
  * @param {String} path
  * @param {Function=} middleware You may also pass multiple middleware.
  * @param {Function} callback
  * @returns {Router}
  */
 Router.prototype.verbs = function (verbs, path, middleware) {
   if (!Array.isArray(verbs) || verbs.length === 0) {
     throw new Error(
       'You have to provide a list of verbs when adding an verbs handler'
     );
   }

   if (typeof path === 'string') {
     middleware = Array.prototype.slice.call(arguments, 2);
   }

   // Sanity check to ensure we have a viable path candidate (eg: string|regex|non-empty array)
   if (
     typeof path !== 'string' &&
     !(path instanceof RegExp) &&
     (!Array.isArray(path) || path.length === 0)
   )
     throw new Error('You have to provide a path when adding an all handler');

   this.register(
     path,
     verbs.map((verb) => verb.toLowerCase()),
     middleware
   );

   return this;
 };

 return Router;
}

where the usage will be like;

const Router = withMultiVerbs('@koa/router');

BTW, you can contribute later to add your module to the recipes section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants